import pygame as pg
import math
import random

class Bean():
    def __init__(self,player,angle = 0,ATK = 20):
        self.img = pg.image.load("images\plants\ProjectilePea.png")
        self.x = player.x
        self.y = player.y
        self.speed = 3
        self.ATK = ATK
        self.angle = angle
        self.rect = (self.x,self.y,self.img.get_width(),self.img.get_height())

    def __contains__(self,item):
        pass

    #子弹击中检测
    def hit(self,**kwargs):
        global Zombies
        Zombies = kwargs[Zombies]
        for e in Zombies:
            if(self.x+20 >=e.x>=self.x-20 and self.y-40<=e.y <=self.y+40):
                
                e.healthy -=self.ATK
                if e.healthy <= 0:
                    try:
                        Zombies.remove(e)
                    except:
                        pass
                if e.healthy <= 20:
                    e.healthbarColor = (255,0,0)
                elif e.healthy > 20 and e.healthy <= 60:
                    e.healthbarColor = (255,255,0)
                return True
        return False
    
    def move(self):
        self.x += self.speed * math.cos(self.angle)
        self.y += self.speed * math.sin(self.angle)
        # self.x += self.speed


class sinBean(Bean):
    def __init__(self, player):
        super().__init__(player)
        self.img = pg.image.load("images\plants\ProjectilePea2.png")

    def move(self):
        self.x += self.speed
        self.y += self.speed * math.cos(math.radians(self.x))*1.2


class bombBean(Bean):
    def __init__(self, player, angle=0):
        super().__init__(player, angle)
        self.img = pg.image.load("images\plants\ProjectilePea3.png")
        self.ATK = 100
        self.sonBean = Bean

    def hit(self,**kwargs):
        global Zombies
        Zombies = kwargs[Zombies]
        for e in Zombies:
            if(self.x >=e.x and self.y-20<=e.y <=self.y+20):
                Beans = [self.sonBean(self,i * 36) for i in range(10)]
                if(self.x >=e.x and self.y-20<=e.y <=self.y+20):
                    e.healthy -=self.ATK
                if e.healthy <= 0:
                    Zombies.remove(e)
                # threading.Thread(target=lambda: self.sonLogic(Beans,Zombies,screen,clock),daemon=True).start()
                # threading.Thread(target=lambda: self.sonDraw(Beans,screen),daemon=True).start()
            if(self.x+20 >= e.x >= self.x-20 and self.y-40 <= e.y <= self.y+40):
                for i in range(10):
                    Beans.append(Bean(self,i * 36))
                e.healthy -= self.ATK
                if e.healthy <= 0:
                    try:
                        Zombies.remove(e)
                    except:
                        pass
                return True
        return False
    


'''
class sinBombBean(sinBean,bombBean):
    """暂时废止"""
    def __init__(self, player):
        super().__init__(player)
        self.sonBean = sinBean
'''